package com.payment.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.payment.common.annotation.Log;
import com.payment.common.base.AjaxResult;
import com.payment.common.enums.BusinessType;
import com.payment.common.page.TableDataInfo;
import com.payment.common.utils.ExcelUtil;
import com.payment.common.utils.StringUtils;
import com.payment.framework.util.ShiroUtils;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.PlanNew;
import com.payment.system.domain.SysUser;
import com.payment.system.service.IPlanNewService;
import com.payment.system.service.ISysUserService;

/**
 * 新计划 信息操作处理
 * 
 * @author payment
 * @date 2019-07-02
 */
@Controller
@RequestMapping("/system/planNew")
public class PlanNewController extends BaseController
{
    private String prefix = "system/planNew";

	@Autowired
	private ISysUserService userService;
	@Autowired
	private IPlanNewService planNewService;
	
	@RequiresPermissions("system:planNew:view")
	@GetMapping()
	public String planNew(ModelMap mmap)
	{
		Long userId= ShiroUtils.getUserId();
		String roleKey=userService.selectUserRoleKey(userId);
		mmap.put("role",roleKey);
		return prefix + "/planNew";
	}
	
	/**
	 * 查询新计划列表
	 */
	@RequiresPermissions("system:planNew:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PlanNew planNew)
	{
		Long userId = ShiroUtils.getUserId();
		SysUser user = ShiroUtils.getSysUser();
		String roleKey = userService.selectUserRoleKey(userId);
		Map<String, Object> map = planNew.getParams();
		List<PlanNew> list = new ArrayList<>();
		if (StringUtils.equals(roleKey, "css")) {
			//客销部门登录
			map.put("css", "css");
			startPage();
			list = planNewService.selectPlanNewList(planNew);
		} else if (StringUtils.equals(roleKey, "agent")) {
			//代理商部门登录
			map.put("agentId", user.getPhonenumber());
			startPage();
			list = planNewService.selectPlanNewList(planNew);
		}else {
			startPage();
			list = planNewService.selectPlanNewListNoEncrypt(planNew);
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出新计划列表
	 */
	@RequiresPermissions("system:planNew:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlanNew planNew)
    {
		Long userId = ShiroUtils.getUserId();
		SysUser user = ShiroUtils.getSysUser();
		String roleKey = userService.selectUserRoleKey(userId);
		Map<String, Object> map = planNew.getParams();
		if (StringUtils.equals(roleKey, "css")) {//客销部门登录
			map.put("css", "css");
		} else if (StringUtils.equals(roleKey, "agent")) {//客销部门登录
			map.put("agentId", user.getPhonenumber());
		}
    	List<PlanNew> list = planNewService.selectPlanNewList(planNew);
        ExcelUtil<PlanNew> util = new ExcelUtil<PlanNew>(PlanNew.class);
        return util.exportExcel(list, "计划信息");
    }
	


	/**
	 * 修改新计划
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		PlanNew planNew = planNewService.selectPlanNewById(id);
		mmap.put("planNew", planNew);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存新计划
	 */
	@RequiresPermissions("system:planNew:edit")
	@Log(title = "新计划", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PlanNew planNew)
	{		
		return toAjax(planNewService.updatePlanNew(planNew));
	}
	
	/**
	 * 删除新计划
	 */
	@RequiresPermissions("system:planNew:remove")
	@Log(title = "新计划", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{

		return toAjax(planNewService.deletePlanNewByIds(ids));
	}
	/**
	 * 禁用或者启用
	 */
	@PostMapping("/updatePlanStatusById")
	@ResponseBody
	public AjaxResult updatePlanStatusById(PlanNew planNew) {
		return planNewService.updatePlanStatusById(planNew);
	}

	/**
	 * 跳转计划明细
	 */
	@GetMapping("/detail/{id}")
	public String detail(@PathVariable("id") String id, ModelMap mmap) {
		PlanNew planNew = planNewService.selectPlanNewById(id);
		if (planNew != null){
			mmap.put("planId",planNew.getId());
			mmap.put("orderId","");
		}
		Long userId=ShiroUtils.getUserId();
		String roleKey=userService.selectUserRoleKey(userId);
		mmap.put("role",roleKey);
		return prefix + "/details";
	}
}
